<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>color_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_color_color_test.js.html">color_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>goog.provide(&#39;goog.colorTest&#39;);
<a name="line15"></a>goog.setTestOnly(&#39;goog.colorTest&#39;);
<a name="line16"></a>
<a name="line17"></a>goog.require(&#39;goog.array&#39;);
<a name="line18"></a>goog.require(&#39;goog.color&#39;);
<a name="line19"></a>goog.require(&#39;goog.color.names&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>function testIsValidColor() {
<a name="line23"></a>  var goodColors = [&#39;#ffffff&#39;, &#39;#ff7812&#39;, &#39;#012345&#39;, &#39;#Ff003D&#39;, &#39;#3CA&#39;,
<a name="line24"></a>                    &#39;(255, 26, 75)&#39;, &#39;RGB(2, 3, 4)&#39;, &#39;(0,0,0)&#39;, &#39;white&#39;,
<a name="line25"></a>                    &#39;blue&#39;];
<a name="line26"></a>  var badColors = [&#39;#xxxxxx&#39;, &#39;8899000&#39;, &#39;not_color&#39;, &#39;#1234567&#39;, &#39;fffffg&#39;,
<a name="line27"></a>                   &#39;(2555,0,0)&#39;, &#39;(1,2,3,4)&#39;, &#39;rgb(1,20,)&#39;, &#39;RGB(20,20,20,)&#39;,
<a name="line28"></a>                   &#39;omgwtfbbq&#39;];
<a name="line29"></a>  for (var i = 0; i &lt; goodColors.length; i++) {
<a name="line30"></a>    assertTrue(goodColors[i], goog.color.isValidColor(goodColors[i]));
<a name="line31"></a>  }
<a name="line32"></a>  for (var i = 0; i &lt; badColors.length; i++) {
<a name="line33"></a>    assertFalse(badColors[i], goog.color.isValidColor(badColors[i]));
<a name="line34"></a>  }
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>function testIsValidHexColor() {
<a name="line39"></a>  var goodHexColors = [&#39;#ffffff&#39;, &#39;#ff7812&#39;, &#39;#012345&#39;, &#39;#Ff003D&#39;, &#39;#3CA&#39;];
<a name="line40"></a>  var badHexColors = [&#39;#xxxxxx&#39;, &#39;889900&#39;, &#39;not_color&#39;, &#39;#1234567&#39;, &#39;fffffg&#39;];
<a name="line41"></a>  for (var i = 0; i &lt; goodHexColors.length; i++) {
<a name="line42"></a>    assertTrue(goodHexColors[i], goog.color.isValidHexColor_(goodHexColors[i]));
<a name="line43"></a>  }
<a name="line44"></a>  for (var i = 0; i &lt; badHexColors.length; i++) {
<a name="line45"></a>    assertFalse(badHexColors[i], goog.color.isValidHexColor_(badHexColors[i]));
<a name="line46"></a>  }
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>function testIsValidRgbColor() {
<a name="line51"></a>  var goodRgbColors = [&#39;(255, 26, 75)&#39;, &#39;RGB(2, 3, 4)&#39;, &#39;(0,0,0)&#39;,
<a name="line52"></a>                       &#39;rgb(255,255,255)&#39;];
<a name="line53"></a>  var badRgbColors = [&#39;(2555,0,0)&#39;, &#39;(1,2,3,4)&#39;, &#39;rgb(1,20,)&#39;,
<a name="line54"></a>                      &#39;RGB(20,20,20,)&#39;];
<a name="line55"></a>  for (var i = 0; i &lt; goodRgbColors.length; i++) {
<a name="line56"></a>    assertEquals(goodRgbColors[i],
<a name="line57"></a>                 goog.color.isValidRgbColor_(goodRgbColors[i]).length, 3);
<a name="line58"></a>  }
<a name="line59"></a>  for (var i = 0; i &lt; badRgbColors.length; i++) {
<a name="line60"></a>    assertEquals(badRgbColors[i],
<a name="line61"></a>                 goog.color.isValidRgbColor_(badRgbColors[i]).length, 0);
<a name="line62"></a>  }
<a name="line63"></a>}
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>function testParse() {
<a name="line67"></a>  var colors = [&#39;rgb(15, 250, 77)&#39;, &#39;(127, 127, 127)&#39;, &#39;#ffeedd&#39;, &#39;123456&#39;,
<a name="line68"></a>                &#39;magenta&#39;];
<a name="line69"></a>  var parsed = goog.array.map(colors, goog.color.parse);
<a name="line70"></a>  assertEquals(&#39;rgb&#39;, parsed[0].type);
<a name="line71"></a>  assertEquals(goog.color.rgbToHex(15, 250, 77), parsed[0].hex);
<a name="line72"></a>  assertEquals(&#39;rgb&#39;, parsed[1].type);
<a name="line73"></a>  assertEquals(goog.color.rgbToHex(127, 127, 127), parsed[1].hex);
<a name="line74"></a>  assertEquals(&#39;hex&#39;, parsed[2].type);
<a name="line75"></a>  assertEquals(&#39;#ffeedd&#39;, parsed[2].hex);
<a name="line76"></a>  assertEquals(&#39;hex&#39;, parsed[3].type);
<a name="line77"></a>  assertEquals(&#39;#123456&#39;, parsed[3].hex);
<a name="line78"></a>  assertEquals(&#39;named&#39;, parsed[4].type);
<a name="line79"></a>  assertEquals(&#39;#ff00ff&#39;, parsed[4].hex);
<a name="line80"></a>
<a name="line81"></a>  var badColors = [&#39;rgb(01, 1, 23)&#39;, &#39;(256, 256, 256)&#39;, &#39;#ffeeddaa&#39;];
<a name="line82"></a>  for (var i = 0; i &lt; badColors.length; i++) {
<a name="line83"></a>    var e = assertThrows(goog.partial(goog.color.parse, badColors[i]));
<a name="line84"></a>    assertContains(&#39;is not a valid color string&#39;, e.message);
<a name="line85"></a>  }
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>function testHexToRgb() {
<a name="line90"></a>  var testColors = [[&#39;#B0FF2D&#39;, [176, 255, 45]],
<a name="line91"></a>                    [&#39;#b26e5f&#39;, [178, 110, 95]],
<a name="line92"></a>                    [&#39;#66f&#39;, [102, 102, 255]]];
<a name="line93"></a>
<a name="line94"></a>  for (var i = 0; i &lt; testColors.length; i++) {
<a name="line95"></a>    var r = goog.color.hexToRgb(testColors[i][0]);
<a name="line96"></a>    var t = testColors[i][1];
<a name="line97"></a>
<a name="line98"></a>    assertEquals(&#39;Red channel should match.&#39;, t[0], r[0]);
<a name="line99"></a>    assertEquals(&#39;Green channel should match.&#39;, t[1], r[1]);
<a name="line100"></a>    assertEquals(&#39;Blue channel should match.&#39;, t[2], r[2]);
<a name="line101"></a>  }
<a name="line102"></a>
<a name="line103"></a>  var badColors = [&#39;&#39;, &#39;#g00&#39;, &#39;some words&#39;];
<a name="line104"></a>  for (var i = 0; i &lt; badColors.length; i++) {
<a name="line105"></a>    var e = assertThrows(goog.partial(goog.color.hexToRgb, badColors[i]));
<a name="line106"></a>    assertEquals(&quot;&#39;&quot; + badColors[i] + &quot;&#39; is not a valid hex color&quot;, e.message);
<a name="line107"></a>  }
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>function testHexToRgbStyle() {
<a name="line112"></a>  assertEquals(&#39;rgb(255,0,0)&#39;, goog.color.hexToRgbStyle(goog.color.names.red));
<a name="line113"></a>  assertEquals(&#39;rgb(206,206,206)&#39;, goog.color.hexToRgbStyle(&#39;#cecece&#39;));
<a name="line114"></a>  assertEquals(&#39;rgb(51,204,170)&#39;, goog.color.hexToRgbStyle(&#39;#3CA&#39;));
<a name="line115"></a>  var badHexColors = [&#39;#1234&#39;, null, undefined, &#39;#.1234567890&#39;];
<a name="line116"></a>  for (var i = 0; i &lt; badHexColors.length; ++i) {
<a name="line117"></a>    var badHexColor = badHexColors[i];
<a name="line118"></a>    var e = assertThrows(goog.partial(goog.color.hexToRgbStyle, badHexColor));
<a name="line119"></a>    assertEquals(&quot;&#39;&quot; + badHexColor + &quot;&#39; is not a valid hex color&quot;, e.message);
<a name="line120"></a>  }
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>function testRgbToHex() {
<a name="line125"></a>  assertEquals(goog.color.names.red, goog.color.rgbToHex(255, 0, 0));
<a name="line126"></a>  assertEquals(&#39;#af13ff&#39;, goog.color.rgbToHex(175, 19, 255));
<a name="line127"></a>  var badRgb = [[-1, -1, -1], [256, 0, 0], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [undefined, 5, 5]];
<a name="line128"></a>  for (var i = 0; i &lt; badRgb.length; ++i) {
<a name="line129"></a>    var e = assertThrows(goog.partial(goog.color.rgbArrayToHex, badRgb[i]));
<a name="line130"></a>    assertContains(&#39;is not a valid RGB color&#39;, e.message);
<a name="line131"></a>  }
<a name="line132"></a>}
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>function testRgbToHsl() {
<a name="line136"></a>  var rgb = [255, 171, 32];
<a name="line137"></a>  var hsl = goog.color.rgbArrayToHsl(rgb);
<a name="line138"></a>  assertEquals(37, hsl[0]);
<a name="line139"></a>  assertTrue(1.0 - hsl[1] &lt; 0.01);
<a name="line140"></a>  assertTrue(hsl[2] - .5625 &lt; 0.01);
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>function testHslToRgb() {
<a name="line145"></a>  var hsl = [60, 0.5, 0.1];
<a name="line146"></a>  var rgb = goog.color.hslArrayToRgb(hsl);
<a name="line147"></a>  assertEquals(38, rgb[0]);
<a name="line148"></a>  assertEquals(38, rgb[1]);
<a name="line149"></a>  assertEquals(13, rgb[2]);
<a name="line150"></a>}
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>// Tests accuracy of HSL to RGB conversion
<a name="line154"></a>function testHSLBidiToRGB() {
<a name="line155"></a>  var DELTA = 1;
<a name="line156"></a>
<a name="line157"></a>  var color = [[100, 56, 200],
<a name="line158"></a>               [255, 0, 0],
<a name="line159"></a>               [0, 0, 255],
<a name="line160"></a>               [0, 255, 0],
<a name="line161"></a>               [255, 255, 255],
<a name="line162"></a>               [0, 0, 0]];
<a name="line163"></a>
<a name="line164"></a>  for (var i = 0; i &lt; color.length; i++) {
<a name="line165"></a>    colorConversionTestHelper(
<a name="line166"></a>        function(color) {
<a name="line167"></a>          return goog.color.rgbToHsl(color[0], color[1], color[2]);
<a name="line168"></a>        },
<a name="line169"></a>        function(color) {
<a name="line170"></a>          return goog.color.hslToRgb(color[0], color[1], color[2]);
<a name="line171"></a>        },
<a name="line172"></a>        color[i], DELTA);
<a name="line173"></a>
<a name="line174"></a>    colorConversionTestHelper(
<a name="line175"></a>        function(color) { return goog.color.rgbArrayToHsl(color); },
<a name="line176"></a>        function(color) { return goog.color.hslArrayToRgb(color); },
<a name="line177"></a>        color[i], DELTA);
<a name="line178"></a>  }
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>// Tests HSV to RGB conversion
<a name="line183"></a>function testHSVToRGB() {
<a name="line184"></a>  var DELTA = 1;
<a name="line185"></a>
<a name="line186"></a>  var color = [[100, 56, 200],
<a name="line187"></a>               [255, 0, 0],
<a name="line188"></a>               [0, 0, 255],
<a name="line189"></a>               [0, 255, 0],
<a name="line190"></a>               [255, 255, 255],
<a name="line191"></a>               [0, 0, 0]];
<a name="line192"></a>
<a name="line193"></a>  for (var i = 0; i &lt; color.length; i++) {
<a name="line194"></a>    colorConversionTestHelper(
<a name="line195"></a>        function(color) {
<a name="line196"></a>          return goog.color.rgbToHsv(color[0], color[1], color[2]);
<a name="line197"></a>        },
<a name="line198"></a>        function(color) {
<a name="line199"></a>          return goog.color.hsvToRgb(color[0], color[1], color[2]);
<a name="line200"></a>        },
<a name="line201"></a>        color[i], DELTA);
<a name="line202"></a>
<a name="line203"></a>    colorConversionTestHelper(
<a name="line204"></a>        function(color) { return goog.color.rgbArrayToHsv(color); },
<a name="line205"></a>        function(color) { return goog.color.hsvArrayToRgb(color); },
<a name="line206"></a>        color[i], DELTA);
<a name="line207"></a>  }
<a name="line208"></a>}
<a name="line209"></a>
<a name="line210"></a>// Tests that HSV space is (0-360) for hue
<a name="line211"></a>function testHSVSpecRangeIsCorrect() {
<a name="line212"></a>  var color = [0, 0, 255];  // Blue is in the middle of hue range
<a name="line213"></a>
<a name="line214"></a>  var hsv = goog.color.rgbToHsv(color[0], color[1], color[2]);
<a name="line215"></a>
<a name="line216"></a>  assertTrue(&quot;H in HSV space looks like it&#39;s not 0-360&quot;, hsv[0] &gt; 1);
<a name="line217"></a>}
<a name="line218"></a>
<a name="line219"></a>// Tests conversion between HSL and Hex
<a name="line220"></a>function testHslToHex() {
<a name="line221"></a>  var DELTA = 1;
<a name="line222"></a>
<a name="line223"></a>  var color = [[0, 0, 0],
<a name="line224"></a>               [20, 0.5, 0.5],
<a name="line225"></a>               [0, 0, 1],
<a name="line226"></a>               [255, .45, .76]];
<a name="line227"></a>
<a name="line228"></a>  for (var i = 0; i &lt; color.length; i++) {
<a name="line229"></a>    colorConversionTestHelper(
<a name="line230"></a>        function(hsl) { return goog.color.hslToHex(hsl[0], hsl[1], hsl[2]); },
<a name="line231"></a>        function(hex) { return goog.color.hexToHsl(hex); },
<a name="line232"></a>        color[i], DELTA);
<a name="line233"></a>
<a name="line234"></a>    colorConversionTestHelper(
<a name="line235"></a>        function(hsl) { return goog.color.hslArrayToHex(hsl); },
<a name="line236"></a>        function(hex) { return goog.color.hexToHsl(hex); },
<a name="line237"></a>        color[i], DELTA);
<a name="line238"></a>  }
<a name="line239"></a>}
<a name="line240"></a>
<a name="line241"></a>// Tests conversion between HSV and Hex
<a name="line242"></a>function testHsvToHex() {
<a name="line243"></a>  var DELTA = 1;
<a name="line244"></a>
<a name="line245"></a>  var color = [[0, 0, 0],
<a name="line246"></a>               [.5, 0.5, 155],
<a name="line247"></a>               [0, 0, 255],
<a name="line248"></a>               [.7, .45, 21]];
<a name="line249"></a>
<a name="line250"></a>  for (var i = 0; i &lt; color.length; i++) {
<a name="line251"></a>    colorConversionTestHelper(
<a name="line252"></a>        function(hsl) { return goog.color.hsvToHex(hsl[0], hsl[1], hsl[2]); },
<a name="line253"></a>        function(hex) { return goog.color.hexToHsv(hex); },
<a name="line254"></a>        color[i], DELTA);
<a name="line255"></a>
<a name="line256"></a>    colorConversionTestHelper(
<a name="line257"></a>        function(hsl) { return goog.color.hsvArrayToHex(hsl); },
<a name="line258"></a>        function(hex) { return goog.color.hexToHsv(hex); },
<a name="line259"></a>        color[i], DELTA);
<a name="line260"></a>  }
<a name="line261"></a>}
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>/**
<a name="line265"></a> * This helper method compares two RGB colors, checking that each color
<a name="line266"></a> * component is the same.
<a name="line267"></a> * @param {Array.&lt;number&gt;} rgb1 Color represented by a 3-element array with
<a name="line268"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line269"></a> * @param {Array.&lt;number&gt;} rgb2 Color represented by a 3-element array with
<a name="line270"></a> *     red, green, and blue values respectively, in the range [0, 255].
<a name="line271"></a> * @return {boolean} True if the colors are the same, false otherwise.
<a name="line272"></a> */
<a name="line273"></a>function rgbColorsAreEqual(rgb1, rgb2) {
<a name="line274"></a>  return (rgb1[0] == rgb2[0] &amp;&amp;
<a name="line275"></a>          rgb1[1] == rgb2[1] &amp;&amp;
<a name="line276"></a>          rgb1[2] == rgb2[2]);
<a name="line277"></a>}
<a name="line278"></a>
<a name="line279"></a>
<a name="line280"></a>/**
<a name="line281"></a> * This method runs unit tests against goog.color.blend().  Test cases include:
<a name="line282"></a> * blending arbitrary colors with factors of 0 and 1, blending the same colors
<a name="line283"></a> * using arbitrary factors, blending different colors of varying factors,
<a name="line284"></a> * and blending colors using factors outside the expected range.
<a name="line285"></a> */
<a name="line286"></a>function testColorBlend() {
<a name="line287"></a>  // Define some RGB colors for our tests.
<a name="line288"></a>  var black = [0, 0, 0];
<a name="line289"></a>  var blue = [0, 0, 255];
<a name="line290"></a>  var gray = [128, 128, 128];
<a name="line291"></a>  var green = [0, 255, 0];
<a name="line292"></a>  var purple = [128, 0, 128];
<a name="line293"></a>  var red = [255, 0, 0];
<a name="line294"></a>  var yellow = [255, 255, 0];
<a name="line295"></a>  var white = [255, 255, 255];
<a name="line296"></a>
<a name="line297"></a>  // Blend arbitrary colors, using 0 and 1 for factors. Using 0 should return
<a name="line298"></a>  // the first color, while using 1 should return the second color.
<a name="line299"></a>  var redWithNoGreen = goog.color.blend(red, green, 1);
<a name="line300"></a>  assertTrue(&#39;red + 0 * green = red&#39;,
<a name="line301"></a>             rgbColorsAreEqual(red, redWithNoGreen));
<a name="line302"></a>  var whiteWithAllBlue = goog.color.blend(white, blue, 0);
<a name="line303"></a>  assertTrue(&#39;white + 1 * blue = blue&#39;,
<a name="line304"></a>             rgbColorsAreEqual(blue, whiteWithAllBlue));
<a name="line305"></a>
<a name="line306"></a>  // Blend the same colors using arbitrary factors. This should return the
<a name="line307"></a>  // same colors.
<a name="line308"></a>  var greenWithGreen = goog.color.blend(green, green, .25);
<a name="line309"></a>  assertTrue(&#39;green + .25 * green = green&#39;,
<a name="line310"></a>             rgbColorsAreEqual(green, greenWithGreen));
<a name="line311"></a>
<a name="line312"></a>  // Blend different colors using varying factors.
<a name="line313"></a>  var blackWithWhite = goog.color.blend(black, white, .5);
<a name="line314"></a>  assertTrue(&#39;black + .5 * white = gray&#39;,
<a name="line315"></a>             rgbColorsAreEqual(gray, blackWithWhite));
<a name="line316"></a>  var redAndBlue = goog.color.blend(red, blue, .5);
<a name="line317"></a>  assertTrue(&#39;red + .5 * blue = purple&#39;,
<a name="line318"></a>             rgbColorsAreEqual(purple, redAndBlue));
<a name="line319"></a>  var lightGreen = goog.color.blend(green, white, .75);
<a name="line320"></a>  assertTrue(&#39;green + .25 * white = a lighter shade of white&#39;,
<a name="line321"></a>             lightGreen[0] &gt; 0 &amp;&amp;
<a name="line322"></a>             lightGreen[1] == 255 &amp;&amp;
<a name="line323"></a>             lightGreen[2] &gt; 0);
<a name="line324"></a>
<a name="line325"></a>  // Blend arbitrary colors using factors outside the expected range.
<a name="line326"></a>  var noGreenAllPurple = goog.color.blend(green, purple, -0.5);
<a name="line327"></a>  assertTrue(&#39;green * -0.5 + purple = purple.&#39;,
<a name="line328"></a>             rgbColorsAreEqual(purple, noGreenAllPurple));
<a name="line329"></a>  var allBlueNoYellow = goog.color.blend(blue, yellow, 1.37);
<a name="line330"></a>  assertTrue(&#39;blue * 1.37 + yellow = blue.&#39;,
<a name="line331"></a>             rgbColorsAreEqual(blue, allBlueNoYellow));
<a name="line332"></a>}
<a name="line333"></a>
<a name="line334"></a>
<a name="line335"></a>/**
<a name="line336"></a> * This method runs unit tests against goog.color.darken(). Test cases
<a name="line337"></a> * include darkening black with arbitrary factors, edge cases (using 0 and 1),
<a name="line338"></a> * darkening colors using various factors, and darkening colors using factors
<a name="line339"></a> * outside the expected range.
<a name="line340"></a> */
<a name="line341"></a>function testColorDarken() {
<a name="line342"></a>  // Define some RGB colors
<a name="line343"></a>  var black = [0, 0, 0];
<a name="line344"></a>  var green = [0, 255, 0];
<a name="line345"></a>  var darkGray = [68, 68, 68];
<a name="line346"></a>  var olive = [128, 128, 0];
<a name="line347"></a>  var purple = [128, 0, 128];
<a name="line348"></a>  var white = [255, 255, 255];
<a name="line349"></a>
<a name="line350"></a>  // Darken black by an arbitrary factor, which should still return black.
<a name="line351"></a>  var darkBlack = goog.color.darken(black, .63);
<a name="line352"></a>  assertTrue(&#39;black darkened by .63 is still black.&#39;,
<a name="line353"></a>             rgbColorsAreEqual(black, darkBlack));
<a name="line354"></a>
<a name="line355"></a>  // Call darken() with edge-case factors (0 and 1).
<a name="line356"></a>  var greenNotDarkened = goog.color.darken(green, 0);
<a name="line357"></a>  assertTrue(&#39;green darkened by 0 is still green.&#39;,
<a name="line358"></a>             rgbColorsAreEqual(green, greenNotDarkened));
<a name="line359"></a>  var whiteFullyDarkened = goog.color.darken(white, 1);
<a name="line360"></a>  assertTrue(&#39;white darkened by 1 is black.&#39;,
<a name="line361"></a>             rgbColorsAreEqual(black, whiteFullyDarkened));
<a name="line362"></a>
<a name="line363"></a>  // Call darken() with various colors and factors. The result should be
<a name="line364"></a>  // a color with less luminance.
<a name="line365"></a>  var whiteHsl = goog.color.rgbToHsl(white[0],
<a name="line366"></a>                                     white[1],
<a name="line367"></a>                                     white[2]);
<a name="line368"></a>  var whiteDarkened = goog.color.darken(white, .43);
<a name="line369"></a>  var whiteDarkenedHsl = goog.color.rgbToHsl(whiteDarkened[0],
<a name="line370"></a>                                             whiteDarkened[1],
<a name="line371"></a>                                             whiteDarkened[2]);
<a name="line372"></a>  assertTrue(&#39;White that\&#39;s darkened has less luminance than white.&#39;,
<a name="line373"></a>             whiteDarkenedHsl[2] &lt; whiteHsl[2]);
<a name="line374"></a>  var purpleHsl = goog.color.rgbToHsl(purple[0],
<a name="line375"></a>                                      purple[1],
<a name="line376"></a>                                      purple[2]);
<a name="line377"></a>  var purpleDarkened = goog.color.darken(purple, .1);
<a name="line378"></a>  var purpleDarkenedHsl = goog.color.rgbToHsl(purpleDarkened[0],
<a name="line379"></a>                                              purpleDarkened[1],
<a name="line380"></a>                                              purpleDarkened[2]);
<a name="line381"></a>  assertTrue(&#39;Purple that\&#39;s darkened has less luminance than purple.&#39;,
<a name="line382"></a>             purpleDarkenedHsl[2] &lt; purpleHsl[2]);
<a name="line383"></a>
<a name="line384"></a>  // Call darken() with factors outside the expected range.
<a name="line385"></a>  var darkGrayTurnedBlack = goog.color.darken(darkGray, 2.1);
<a name="line386"></a>  assertTrue(&#39;Darkening dark gray by 2.1 returns black.&#39;,
<a name="line387"></a>             rgbColorsAreEqual(black, darkGrayTurnedBlack));
<a name="line388"></a>  var whiteNotDarkened = goog.color.darken(white, -0.62);
<a name="line389"></a>  assertTrue(&#39;Darkening white by -0.62 returns white.&#39;,
<a name="line390"></a>             rgbColorsAreEqual(white, whiteNotDarkened));
<a name="line391"></a>}
<a name="line392"></a>
<a name="line393"></a>
<a name="line394"></a>/**
<a name="line395"></a> * This method runs unit tests against goog.color.lighten(). Test cases
<a name="line396"></a> * include lightening white with arbitrary factors, edge cases (using 0 and 1),
<a name="line397"></a> * lightening colors using various factors, and lightening colors using factors
<a name="line398"></a> * outside the expected range.
<a name="line399"></a> */
<a name="line400"></a>function testColorLighten() {
<a name="line401"></a>  // Define some RGB colors
<a name="line402"></a>  var black = [0, 0, 0];
<a name="line403"></a>  var brown = [165, 42, 42];
<a name="line404"></a>  var navy = [0, 0, 128];
<a name="line405"></a>  var orange = [255, 165, 0];
<a name="line406"></a>  var white = [255, 255, 255];
<a name="line407"></a>
<a name="line408"></a>  // Lighten white by an arbitrary factor, which should still return white.
<a name="line409"></a>  var lightWhite = goog.color.lighten(white, .41);
<a name="line410"></a>  assertTrue(&#39;white lightened by .41 is still white.&#39;,
<a name="line411"></a>             rgbColorsAreEqual(white, lightWhite));
<a name="line412"></a>
<a name="line413"></a>  // Call lighten() with edge-case factors(0 and 1).
<a name="line414"></a>  var navyNotLightened = goog.color.lighten(navy, 0);
<a name="line415"></a>  assertTrue(&#39;navy lightened by 0 is still navy.&#39;,
<a name="line416"></a>             rgbColorsAreEqual(navy, navyNotLightened));
<a name="line417"></a>  var orangeFullyLightened = goog.color.lighten(orange, 1);
<a name="line418"></a>  assertTrue(&#39;orange lightened by 1 is white.&#39;,
<a name="line419"></a>             rgbColorsAreEqual(white, orangeFullyLightened));
<a name="line420"></a>
<a name="line421"></a>  // Call lighten() with various colors and factors. The result should be
<a name="line422"></a>  // a color with greater luminance.
<a name="line423"></a>  var blackHsl = goog.color.rgbToHsl(black[0],
<a name="line424"></a>                                     black[1],
<a name="line425"></a>                                     black[2]);
<a name="line426"></a>  var blackLightened = goog.color.lighten(black, .33);
<a name="line427"></a>  var blackLightenedHsl = goog.color.rgbToHsl(blackLightened[0],
<a name="line428"></a>                                              blackLightened[1],
<a name="line429"></a>                                              blackLightened[2]);
<a name="line430"></a>  assertTrue(&#39;Black that\&#39;s lightened has more luminance than black.&#39;,
<a name="line431"></a>             blackLightenedHsl[2] &gt;= blackHsl[2]);
<a name="line432"></a>  var orangeHsl = goog.color.rgbToHsl(orange[0],
<a name="line433"></a>                                      orange[1],
<a name="line434"></a>                                      orange[2]);
<a name="line435"></a>  var orangeLightened = goog.color.lighten(orange, .91);
<a name="line436"></a>  var orangeLightenedHsl = goog.color.rgbToHsl(orangeLightened[0],
<a name="line437"></a>                                               orangeLightened[1],
<a name="line438"></a>                                               orangeLightened[2]);
<a name="line439"></a>  assertTrue(&#39;Orange that\&#39;s lightened has more luminance than orange.&#39;,
<a name="line440"></a>             orangeLightenedHsl[2] &gt;= orangeHsl[2]);
<a name="line441"></a>
<a name="line442"></a>  // Call lighten() with factors outside the expected range.
<a name="line443"></a>  var navyTurnedWhite = goog.color.lighten(navy, 1.01);
<a name="line444"></a>  assertTrue(&#39;Lightening navy by 1.01 returns white.&#39;,
<a name="line445"></a>             rgbColorsAreEqual(white, navyTurnedWhite));
<a name="line446"></a>  var brownNotLightened = goog.color.lighten(brown, -0.0000001);
<a name="line447"></a>  assertTrue(&#39;Lightening brown by -0.0000001 returns brown.&#39;,
<a name="line448"></a>             rgbColorsAreEqual(brown, brownNotLightened));
<a name="line449"></a>}
<a name="line450"></a>
<a name="line451"></a>
<a name="line452"></a>/**
<a name="line453"></a> * This method runs unit tests against goog.color.hslDistance().
<a name="line454"></a> */
<a name="line455"></a>function testHslDistance() {
<a name="line456"></a>  // Define some HSL colors
<a name="line457"></a>  var aliceBlueHsl = goog.color.rgbToHsl(240, 248, 255);
<a name="line458"></a>  var blackHsl = goog.color.rgbToHsl(0, 0, 0);
<a name="line459"></a>  var ghostWhiteHsl = goog.color.rgbToHsl(248, 248, 255);
<a name="line460"></a>  var navyHsl = goog.color.rgbToHsl(0, 0, 128);
<a name="line461"></a>  var redHsl = goog.color.rgbToHsl(255, 0, 0);
<a name="line462"></a>  var whiteHsl = goog.color.rgbToHsl(255, 255, 255);
<a name="line463"></a>
<a name="line464"></a>  // The distance between the same colors should be 0.
<a name="line465"></a>  assertTrue(&#39;There is no HSL distance between white and white.&#39;,
<a name="line466"></a>             goog.color.hslDistance(whiteHsl, whiteHsl) == 0);
<a name="line467"></a>  assertTrue(&#39;There is no HSL distance between red and red.&#39;,
<a name="line468"></a>             goog.color.hslDistance(redHsl, redHsl) == 0);
<a name="line469"></a>
<a name="line470"></a>  // The distance between various colors should be within certain thresholds.
<a name="line471"></a>  var hslDistance = goog.color.hslDistance(whiteHsl, ghostWhiteHsl);
<a name="line472"></a>  assertTrue(&#39;The HSL distance between white and ghost white is &gt; 0.&#39;,
<a name="line473"></a>             hslDistance &gt; 0);
<a name="line474"></a>  assertTrue(&#39;The HSL distance between white and ghost white is &lt;= 0.02.&#39;,
<a name="line475"></a>             hslDistance &lt;= 0.02);
<a name="line476"></a>  hslDistance = goog.color.hslDistance(whiteHsl, redHsl);
<a name="line477"></a>  assertTrue(&#39;The HSL distance betwen white and red is &gt; 0.02.&#39;,
<a name="line478"></a>             hslDistance &gt; 0.02);
<a name="line479"></a>  hslDistance = goog.color.hslDistance(navyHsl, aliceBlueHsl);
<a name="line480"></a>  assertTrue(&#39;The HSL distance between navy and alice blue is &gt; 0.02.&#39;,
<a name="line481"></a>             hslDistance &gt; 0.02);
<a name="line482"></a>  hslDistance = goog.color.hslDistance(blackHsl, whiteHsl);
<a name="line483"></a>  assertTrue(&#39;The HSL distance between white and black is 1.&#39;,
<a name="line484"></a>             hslDistance == 1);
<a name="line485"></a>}
<a name="line486"></a>
<a name="line487"></a>
<a name="line488"></a>/**
<a name="line489"></a> * This method runs unit tests against goog.color.yiqBrightness_().
<a name="line490"></a> */
<a name="line491"></a>function testYiqBrightness() {
<a name="line492"></a>  var white = [255, 255, 255];
<a name="line493"></a>  var black = [0, 0, 0];
<a name="line494"></a>  var coral = [255, 127, 80];
<a name="line495"></a>  var lightgreen = [144, 238, 144];
<a name="line496"></a>
<a name="line497"></a>  var whiteBrightness = goog.color.yiqBrightness_(white);
<a name="line498"></a>  var blackBrightness = goog.color.yiqBrightness_(black);
<a name="line499"></a>  var coralBrightness = goog.color.yiqBrightness_(coral);
<a name="line500"></a>  var lightgreenBrightness = goog.color.yiqBrightness_(lightgreen);
<a name="line501"></a>
<a name="line502"></a>  // brightness should be a number
<a name="line503"></a>  assertTrue(&#39;White brightness is a number.&#39;,
<a name="line504"></a>             typeof whiteBrightness == &#39;number&#39;);
<a name="line505"></a>  assertTrue(&#39;Coral brightness is a number.&#39;,
<a name="line506"></a>             typeof coralBrightness == &#39;number&#39;);
<a name="line507"></a>
<a name="line508"></a>  // brightness for known colors should match known values
<a name="line509"></a>  assertEquals(&#39;White brightness is 255&#39;, whiteBrightness, 255);
<a name="line510"></a>  assertEquals(&#39;Black brightness is 0&#39;, blackBrightness, 0);
<a name="line511"></a>  assertEquals(&#39;Coral brightness is 160&#39;, coralBrightness, 160);
<a name="line512"></a>  assertEquals(&#39;Lightgreen brightness is 199&#39;, lightgreenBrightness, 199);
<a name="line513"></a>}
<a name="line514"></a>
<a name="line515"></a>
<a name="line516"></a>/**
<a name="line517"></a> * This method runs unit tests against goog.color.yiqBrightnessDiff_().
<a name="line518"></a> */
<a name="line519"></a>function testYiqBrightnessDiff() {
<a name="line520"></a>  var colors = {
<a name="line521"></a>    &#39;deeppink&#39;: [255, 20, 147],
<a name="line522"></a>    &#39;indigo&#39;: [75, 0, 130],
<a name="line523"></a>    &#39;saddlebrown&#39;: [139, 69, 19]
<a name="line524"></a>  };
<a name="line525"></a>
<a name="line526"></a>  var diffs = new Object();
<a name="line527"></a>  for (name1 in colors) {
<a name="line528"></a>    for (name2 in colors) {
<a name="line529"></a>      diffs[name1 + &#39;-&#39; + name2] =
<a name="line530"></a>          goog.color.yiqBrightnessDiff_(colors[name1], colors[name2]);
<a name="line531"></a>    }
<a name="line532"></a>  }
<a name="line533"></a>
<a name="line534"></a>  for (pair in diffs) {
<a name="line535"></a>    // each brightness diff should be a number
<a name="line536"></a>    assertTrue(pair + &#39; diff is a number.&#39;, typeof diffs[pair] == &#39;number&#39;);
<a name="line537"></a>    // each brightness diff should be greater than or equal to 0
<a name="line538"></a>    assertTrue(pair + &#39; diff is greater than or equal to 0.&#39;, diffs[pair] &gt;= 0);
<a name="line539"></a>  }
<a name="line540"></a>
<a name="line541"></a>  // brightness diff for same-color pairs should be 0
<a name="line542"></a>  assertEquals(&#39;deeppink-deeppink is 0.&#39;, diffs[&#39;deeppink-deeppink&#39;], 0);
<a name="line543"></a>  assertEquals(&#39;indigo-indigo is 0.&#39;, diffs[&#39;indigo-indigo&#39;], 0);
<a name="line544"></a>
<a name="line545"></a>  // brightness diff for known pairs should match known values
<a name="line546"></a>  assertEquals(&#39;deeppink-indigo is 68.&#39;, diffs[&#39;deeppink-indigo&#39;], 68);
<a name="line547"></a>  assertEquals(&#39;saddlebrown-deeppink is 21.&#39;,
<a name="line548"></a>               diffs[&#39;saddlebrown-deeppink&#39;], 21);
<a name="line549"></a>
<a name="line550"></a>  // reversed pairs should have equal values
<a name="line551"></a>  assertEquals(&#39;indigo-saddlebrown is 47.&#39;, diffs[&#39;indigo-saddlebrown&#39;], 47);
<a name="line552"></a>  assertEquals(&#39;saddlebrown-indigo is also 47.&#39;,
<a name="line553"></a>               diffs[&#39;saddlebrown-indigo&#39;], 47);
<a name="line554"></a>}
<a name="line555"></a>
<a name="line556"></a>
<a name="line557"></a>/**
<a name="line558"></a> * This method runs unit tests against goog.color.colorDiff_().
<a name="line559"></a> */
<a name="line560"></a>function testColorDiff() {
<a name="line561"></a>  var colors = {
<a name="line562"></a>    &#39;mediumblue&#39;: [0, 0, 205],
<a name="line563"></a>    &#39;oldlace&#39;: [253, 245, 230],
<a name="line564"></a>    &#39;orchid&#39;: [218, 112, 214]
<a name="line565"></a>  };
<a name="line566"></a>
<a name="line567"></a>  var diffs = new Object();
<a name="line568"></a>  for (name1 in colors) {
<a name="line569"></a>    for (name2 in colors) {
<a name="line570"></a>      diffs[name1 + &#39;-&#39; + name2] =
<a name="line571"></a>          goog.color.colorDiff_(colors[name1], colors[name2]);
<a name="line572"></a>    }
<a name="line573"></a>  }
<a name="line574"></a>
<a name="line575"></a>  for (pair in diffs) {
<a name="line576"></a>    // each color diff should be a number
<a name="line577"></a>    assertTrue(pair + &#39; diff is a number.&#39;, typeof diffs[pair] == &#39;number&#39;);
<a name="line578"></a>    // each color diff should be greater than or equal to 0
<a name="line579"></a>    assertTrue(pair + &#39; diff is greater than or equal to 0.&#39;, diffs[pair] &gt;= 0);
<a name="line580"></a>  }
<a name="line581"></a>
<a name="line582"></a>  // color diff for same-color pairs should be 0
<a name="line583"></a>  assertEquals(&#39;mediumblue-mediumblue is 0.&#39;,
<a name="line584"></a>               diffs[&#39;mediumblue-mediumblue&#39;], 0);
<a name="line585"></a>  assertEquals(&#39;oldlace-oldlace is 0.&#39;, diffs[&#39;oldlace-oldlace&#39;], 0);
<a name="line586"></a>
<a name="line587"></a>  // color diff for known pairs should match known values
<a name="line588"></a>  assertEquals(&#39;mediumblue-oldlace is 523.&#39;, diffs[&#39;mediumblue-oldlace&#39;], 523);
<a name="line589"></a>  assertEquals(&#39;oldlace-orchid is 184.&#39;, diffs[&#39;oldlace-orchid&#39;], 184);
<a name="line590"></a>
<a name="line591"></a>  // reversed pairs should have equal values
<a name="line592"></a>  assertEquals(&#39;orchid-mediumblue is 339.&#39;, diffs[&#39;orchid-mediumblue&#39;], 339);
<a name="line593"></a>  assertEquals(&#39;mediumblue-orchid is also 339.&#39;,
<a name="line594"></a>               diffs[&#39;mediumblue-orchid&#39;], 339);
<a name="line595"></a>}
<a name="line596"></a>
<a name="line597"></a>
<a name="line598"></a>/**
<a name="line599"></a> * This method runs unit tests against goog.color.highContrast().
<a name="line600"></a> */
<a name="line601"></a>function testHighContrast() {
<a name="line602"></a>  white = [255, 255, 255];
<a name="line603"></a>  black = [0, 0, 0];
<a name="line604"></a>  lemonchiffron = [255, 250, 205];
<a name="line605"></a>  sienna = [160, 82, 45];
<a name="line606"></a>
<a name="line607"></a>  var suggestion = goog.color.highContrast(
<a name="line608"></a>      black, [white, black, sienna, lemonchiffron]);
<a name="line609"></a>
<a name="line610"></a>  // should return an array of three numbers
<a name="line611"></a>  assertTrue(&#39;Return value is an array.&#39;, typeof suggestion == &#39;object&#39;);
<a name="line612"></a>  assertTrue(&#39;Return value is 3 long.&#39;, suggestion.length == 3);
<a name="line613"></a>
<a name="line614"></a>  // known color combos should return a known (i.e. human-verified) suggestion
<a name="line615"></a>  assertArrayEquals(&#39;White is best on sienna.&#39;,
<a name="line616"></a>      goog.color.highContrast(
<a name="line617"></a>          sienna, [white, black, sienna, lemonchiffron]), white);
<a name="line618"></a>  assertArrayEquals(&#39;Black is best on lemonchiffron.&#39;,
<a name="line619"></a>      goog.color.highContrast(
<a name="line620"></a>          white, [white, black, sienna, lemonchiffron]), black);
<a name="line621"></a>}
<a name="line622"></a>
<a name="line623"></a>
<a name="line624"></a>/**
<a name="line625"></a> * Helper function for color conversion functions between two colorspaces.
<a name="line626"></a> * @param {Function} funcOne Function that converts from 1st colorspace to 2nd
<a name="line627"></a> * @param {Function} funcTwo Function that converts from 2nd colorspace to 2nd
<a name="line628"></a> * @param {Array.&lt;number&gt;} color The color array passed to funcOne
<a name="line629"></a> * @param {number} DELTA Margin of error for each element in color
<a name="line630"></a> */
<a name="line631"></a>function colorConversionTestHelper(funcOne, funcTwo, color, DELTA) {
<a name="line632"></a>
<a name="line633"></a>  var temp = funcOne(color);
<a name="line634"></a>
<a name="line635"></a>  if (!goog.color.isValidHexColor_(temp)) {
<a name="line636"></a>    assertTrue(&#39;First conversion had a NaN: &#39; + temp, !isNaN(temp[0]));
<a name="line637"></a>    assertTrue(&#39;First conversion had a NaN: &#39; + temp, !isNaN(temp[1]));
<a name="line638"></a>    assertTrue(&#39;First conversion had a NaN: &#39; + temp, !isNaN(temp[2]));
<a name="line639"></a>  }
<a name="line640"></a>
<a name="line641"></a>  var back = funcTwo(temp);
<a name="line642"></a>
<a name="line643"></a>  if (!goog.color.isValidHexColor_(temp)) {
<a name="line644"></a>    assertTrue(&#39;Second conversion had a NaN: &#39; + back, !isNaN(back[0]));
<a name="line645"></a>    assertTrue(&#39;Second conversion had a NaN: &#39; + back, !isNaN(back[1]));
<a name="line646"></a>    assertTrue(&#39;Second conversion had a NaN: &#39; + back, !isNaN(back[2]));
<a name="line647"></a>  }
<a name="line648"></a>
<a name="line649"></a>  assertColorFuzzyEquals(&#39;Color was off&#39;, color, back, DELTA);
<a name="line650"></a>}
<a name="line651"></a>
<a name="line652"></a>
<a name="line653"></a>/**
<a name="line654"></a> * Checks equivalence between two colors&#39; respective values.  Accepts +- delta
<a name="line655"></a> * for each pair of values
<a name="line656"></a> * @param {string} Str
<a name="line657"></a> * @param {Array.&lt;number&gt;} expected
<a name="line658"></a> * @param {Array.&lt;number&gt;} actual
<a name="line659"></a> * @param {number} delta Margin of error for each element in color array
<a name="line660"></a> */
<a name="line661"></a>function assertColorFuzzyEquals(str, expected, actual, delta) {
<a name="line662"></a>  assertTrue(str + &#39; Expected: &#39; + expected + &#39;  and got: &#39; + actual +
<a name="line663"></a>                 &#39; w/ delta: &#39; + delta,
<a name="line664"></a>             (Math.abs(expected[0] - actual[0]) &lt;= delta) &amp;&amp;
<a name="line665"></a>             (Math.abs(expected[1] - actual[1]) &lt;= delta) &amp;&amp;
<a name="line666"></a>             (Math.abs(expected[2] - actual[2]) &lt;= delta));
<a name="line667"></a>}
</pre>


</body>
</html>
